<?php
require_once("../DAO/UserDAO.php");
include_once("../DAO/ParagraphDAO.php");
include_once("../Utility/WebUtilities.php");
include_once("../Utility/WebSession.php");

if(WebSession::CurrentUserNoReturn() == null || WebSession::CurrentUserNoReturn()->Role != WebUtilities::GetAdminRoleId())
{
	WebUtilities::Redirect("index.php");
}

$paragraphDAO = new ParagraphDAO();

$pageSize = WebUtilities::GetPageSize();
$page = WebUtilities::GetQueryString("page", 1);
$totalPage = 0;
$paragraphList = $paragraphDAO->GetParagraphs($page, $pageSize, $totalPage);

$paragraphStr = "";
if(count($paragraphList) == 0)
{
	$paragraphStr = "<tr><td colspan='5'>No items</td></tr>";
}
else
{
	for($i = 0; $i < count($paragraphList); $i++)
	{
		$item = $paragraphList[$i];
		if($item->Sound != "")
		{
			$soundStr = "<td>Yes</td>";
		}
		else
		{
			$soundStr = "<td>No</td>";
		}
		$paragraphStr = $paragraphStr . "<tr><td class='action'><input type='checkbox' onclick='checkProcess.checkItem(this.checked)' class='checkItems' name='checkItems" . $i . "' value='" . $item->Id . "'/></td>"
									. "<td>" . $item->Id . "</td>"
									. "<td><a href='#' onclick=\"viewParagraphContent('" . $item->Id . "')\">View Detail</a></td>"
									. $soundStr
									. "<td><a href='EditParagraph.php?id=" . $item->Id . "'>Edit</a></td></tr>"; 
	}
}

$submitUrl = "ParagraphManagement.php?page=" . $page;
$pageUrlFirst = "<a href='ParagraphManagement.php?page=1'>First</a>";
$pageUrlLast = "<a href='ParagraphManagement.php?page=" . $totalPage ."'>Last</a>";
$pageUrlNext = "<a href='ParagraphManagement.php?page=" . ($page + 1) ."'>Next ></a>";
$pageUrlPrev = "<a href='ParagraphManagement.php?page=" . ($page - 1) ."'>< Prev</a>";
if($page <= 1)
{
	$pageUrlFirst = "First";
	$pageUrlPrev = "< Prev";
}
if($page >= $totalPage)
{
	$pageUrlLast = "Last";
	$pageUrlNext = "Next >";
}

$mainContent = "<form enctype='multipart/form-data' action='" . $submitUrl ."' method='POST'>
					<table class='manager' id='tableAnswers'>
						<thead>
							<tr>
								<th><input type='checkbox' id='checkAllItem' name='checkAllItem' onclick='checkProcess.checkAll(this.checked)' /></th>
								<th>Paragraph Id</th>
								<th>Content</th>
								<th>Sound</th>
								<th>Action</th>
							</tr>
						</thead>
						<tbody class='tableBody'>"
							. $paragraphStr . 
						"</tbody>
					</table>" .
				"<div class='paging'>
					Page " . ($page >= $totalPage ? $totalPage : $page) . " of " . $totalPage . " " . 
					$pageUrlFirst . " | " . $pageUrlPrev . " | " . $pageUrlNext . " | " . $pageUrlLast . 
				"</div><div class='paging'>
					<div>
						<input type='button' value='Delete' name='btDelete' />
						<input type='button' value='Add' onclick=\"window.location='EditParagraph.php';\" />
					</div>
				</div></form>
				<div id='popupPanel' class='popupPanel'>
					<a href='#' onclick='ClosePopupPanel(); return false;'>Close</a>
					<div id='panelContent' style='padding:10px 10px 10px 10px;overflow:auto;'></div>
				</div>";
$scriptContent = "checkProcess.totalItems = " . count($paragraphList) . ";" . 
	"checkProcess.checkAllItems = document.getElementById('checkAllItem');";
$pageIndex = 4;
include("MasterPage.php");

?>